package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.view.View;
import android.widget.RadioButton;

import com.avos.avoscloud.AVCloud;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVRelation;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.FunctionCallback;
import com.avos.avoscloud.SaveCallback;
import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.OrderCreateRequestDto;
import com.wqtz.chechai.bean.OrderPeriodRequestDto;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.LogUtils;
import com.wqtz.chechai.view.fragments.MessageFragment;
import com.wqtz.chechai.view.fragments.MoreFragment;
import com.wqtz.chechai.view.fragments.QuoteFragment;
import com.wqtz.chechai.view.fragments.ToolsFragment;
import com.wqtz.chechai.widget.BadgerTextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/12 11:49
 */
public class MainActivity extends BaseActivity {
	public static final AVUser CURRENT_USER = AVUser.getCurrentUser();
	private static OrderCreateRequestDto orderCreateRequestDto;
	@BindView(R.id.rb_quote)
	RadioButton mRbQuote;
	@BindView(R.id.rb_message)
	RadioButton mRbMessage;
	@BindView(R.id.rb_new_add)
	RadioButton mRbNewAdd;
	@BindView(R.id.rb_settings)
	RadioButton mRbSettings;
	@BindView(R.id.rb_more)
	RadioButton mRbMore;
	@BindView(R.id.tvMessageDot)
	BadgerTextView mTvMessageDot;
	private Intent mApprovalIntent;
	private Object carAssess;


	@Override
	public void init() {
		ButterKnife.bind(this); //懒人框架初始化
		showMessage(0);
		//testOrderCreate(); //创建订单信息
		//getOrderData();// 获取报价列表
		//testOrderPeriodRequestDto();//创建借款信息
		getOrderDetail(); //获取报价详情

	}

	private void testOrderPeriodRequestDto() {
		OrderPeriodRequestDto orderPeriod = new OrderPeriodRequestDto();
		orderPeriod.orderId = "orderId";
		orderPeriod.dictPeriodId = "dictPeriodId";
		orderPeriod.number = "苏A12345";
		orderPeriod.color = "黑色";
		orderPeriod.numberOfkeys = 1;
		orderPeriod.mobile = "18159020773";
		OrderPeriodRequestDto.IdentityCardBean idbean = new OrderPeriodRequestDto.IdentityCardBean();
		idbean.name = "张三";
		idbean.gender = "female";
		idbean.nation = "汉族";
		idbean.birth = "2016-01-01";
		idbean.address = "福建省鼓楼区";
		idbean.number = "352228199308212510";
		idbean.issuingAuthority = "福州公安局";
		idbean.usefulLifeBegin = "2016-01-01";
		idbean.usefulLifeEnd = "2036-01-01";
		orderPeriod.setIdentityCard(idbean);

		Map<String, Object> params = new HashMap<>();
		params.put("orderPeriod", orderPeriod);

		AVCloud.callFunctionInBackground("order.period", params, new FunctionCallback() {
			public void done(Object object, AVException e) {
				if (e == null) {
					Logger.e("成功" + object.toString());
				} else {
					// 处理报错
					Logger.e("失败" + e.getMessage() + e.getMessage());
				}
			}
		});

	}

	public void getOrderData() {
		final AVQuery<AVObject> query = new AVQuery<>("Order");
		query.whereEqualTo("user", AVObject.createWithoutData("_User", AVUser.getCurrentUser().getObjectId()));
		query.limit(10);
		query.orderByDescending("updatedAt");
		query.include("carAssess");// 关键代码，用 include 告知服务端需要返回的关联属性对应的对象的详细信息，而不仅仅是 objectId
		query.findInBackground(new FindCallback<AVObject>() {
			@Override
			public void done(List<AVObject> list, AVException e) {
				// list 是最近的十条报价, 其 carAssess 字段也有相应数据
				for (AVObject order : list) {//Order表中相关数据
					// 并不需要网络访问
					AVObject avObject = order.getAVObject("carAssess");//CarAssess 相关数据
					Logger.e(avObject.toString());
				}
				Logger.e(list.size() + "列表长度 用户ID" + AVUser.getCurrentUser().getObjectId());
			}
		});
	}

	public static void testOrderCreate() {

		orderCreateRequestDto = new OrderCreateRequestDto();

		orderCreateRequestDto.setVin("LSVAM4187C2184847");
		orderCreateRequestDto.setBody("body");
		orderCreateRequestDto.setBrandId("brandId");
		orderCreateRequestDto.setBrandName("大众");
		orderCreateRequestDto.setSeriesId("seriesId");
		orderCreateRequestDto.setSeriesName("朗逸");
		orderCreateRequestDto.setSepcId("sepcId");
		orderCreateRequestDto.setSepcName("科技系列");

		orderCreateRequestDto.setMortgageStatus("first");
		orderCreateRequestDto.setMileage(10);
		orderCreateRequestDto.setProcurePrice(20);
		orderCreateRequestDto.setSellingPrice(30);
		orderCreateRequestDto.setPersionalPrice(40);
		orderCreateRequestDto.setEstimatePrice(50);
		orderCreateRequestDto.setUsedMonth("04");
		orderCreateRequestDto.setUsedYear("2012");

		List<OrderCreateRequestDto.CarQuestionsBean> carQuestionCreateDtoList = new ArrayList<>();
		OrderCreateRequestDto.CarQuestionsBean carQuestionCreateDto = new OrderCreateRequestDto.CarQuestionsBean();
		List<String> photosList = new ArrayList<>();
		photosList.add("photo1");
		photosList.add("photo2");
		carQuestionCreateDto.setType("wz");
		carQuestionCreateDto.setDeduct(50);
		carQuestionCreateDto.setExtData("extData");
		carQuestionCreateDto.setPhotos(photosList);
		List<String> drivingLicenseList = new ArrayList<>();
		drivingLicenseList.add("drivingLicense1");
		drivingLicenseList.add("drivingLicense2");
		List<String> copyDriverLicenseList = new ArrayList<>();
		copyDriverLicenseList.add("copyDriverLicense1");
		copyDriverLicenseList.add("copyDriverLicense2");

		orderCreateRequestDto.setCarQuestions(carQuestionCreateDtoList);
		orderCreateRequestDto.setDrivingLicense(drivingLicenseList);
		orderCreateRequestDto.setCopyDriverLicense(copyDriverLicenseList);

		Map<String, Object> params = new HashMap<>();
		params.put("orderCreate", orderCreateRequestDto);

		AVCloud.callFunctionInBackground("order.create", params, new FunctionCallback() {
			public void done(Object object, AVException e) {
				if (e == null) {
					Logger.e("成功" + object.toString());
				} else {
					// 处理报错
					Logger.e("失败" + e.getMessage() + e.getMessage());
				}
			}
		});

		//putData();

	}

	private static void putData() {

		AVObject product = new AVObject("TestorderCreate");
		product.put("vin", orderCreateRequestDto.getVin());
		product.put("body", orderCreateRequestDto.getBody());
		product.put("brandId", orderCreateRequestDto.getBrandId());
		product.put("brandName", orderCreateRequestDto.getBrandName());
		product.put("seriesId", orderCreateRequestDto.getSepcId());
		product.put("seriesName", orderCreateRequestDto.getSeriesName());
		product.put("sepcId", orderCreateRequestDto.getSepcId());
		product.put("sepcName", orderCreateRequestDto.getSepcName());
		product.put("usedYear", orderCreateRequestDto.getUsedYear());
		product.put("usedMonth", orderCreateRequestDto.getUsedMonth());
		product.put("mileage", orderCreateRequestDto.getMileage());
		product.put("mortgageStatus", orderCreateRequestDto.getMortgageStatus());
		product.put("sellingPrice", orderCreateRequestDto.getSellingPrice());
		product.put("persionalPrice", orderCreateRequestDto.getPersionalPrice());
		product.put("estimatePrice", orderCreateRequestDto.getEstimatePrice());
		product.put("CarQuestions", orderCreateRequestDto.getCarQuestions());
		product.put("drivingLicense", orderCreateRequestDto.getDrivingLicense());
		product.put("copyDriverLicense", orderCreateRequestDto.getCopyDriverLicense());
		product.saveInBackground(new SaveCallback() {
			@Override
			public void done(AVException e) {
				if (e == null) {
					Logger.e("成功");
				} else {
					Logger.e("失败");
				}
			}
		});
	}


	@Override
	public void loadData() {
		//默认报价页面
		fragmentManager(R.id.framlayout_main, new QuoteFragment(), "QuoteFragment");
	}

	@Override
	public int setContentViews() {
		return R.layout.activity_main;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}

	@OnClick({R.id.rb_quote, R.id.rb_message, R.id.rb_new_add, R.id.rb_settings, R.id.rb_more})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.rb_quote:
				this.fragmentManager(R.id.framlayout_main, new QuoteFragment(), "QuoteFragment");
				break;
			case R.id.rb_message:
				hideMessage(true);
				mTvMessageDot.showBadger(false);
				this.fragmentManager(R.id.framlayout_main, new MessageFragment(), "MessageFragment");
				break;
			case R.id.rb_new_add:
				Intent intent = new Intent(this, NewAddActivity.class);
				startActivity(intent);
			case R.id.rb_settings:
				this.fragmentManager(R.id.framlayout_main, new ToolsFragment(), "ToolsFragment");
				break;
			case R.id.rb_more:
				this.fragmentManager(R.id.framlayout_main, new MoreFragment(), "MoreFragment");
				break;
		}
	}

	//展示消息
	public void showMessage(int count) {
		//设置消息条数显示0表示小红点
		mTvMessageDot.showBadger(true);
		mTvMessageDot.setBadgerNumber(count);
	}

	//是否展示消息
	public void hideMessage(Boolean isShow) {
		mTvMessageDot.showBadger(isShow);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	//activity跳转到另一个activity中的fragment，getIntent()获取不到值重写该方法
	@Override
	protected void onNewIntent(Intent intent) {
		mApprovalIntent = intent;
		super.onNewIntent(intent);
	}

	@Override
	protected void onResume() {
		super.onResume();
		if (mApprovalIntent != null) {
			int id = mApprovalIntent.getIntExtra("id", -1);
			LogUtils.e("id:" + id);
			if (id == 1) {
				//加载动态页面
				fragmentManager(R.id.framlayout_main, new MessageFragment(), "MessageFragment");
			} else {
				//默认报价页面
				fragmentManager(R.id.framlayout_main, new QuoteFragment(), "QuoteFragment");
				mRbQuote.setChecked(true);
			}
		}
		activitys.add(this); //添加当前的activity
	}

	public void getOrderDetail() {

	}
}